package com.trg.flex.frm
{
	import com.trg.flex.FlexUtils;
	import com.trg.flex.MetaDataUtils;
	import com.trg.flex.frm.validation.InputValidationHelper;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.controls.TextInput;
	import mx.controls.dataGridClasses.DataGridListData;
	import mx.events.FlexEvent;
	import mx.events.PropertyChangeEvent;

	[Event(name="validChanged", type="mx.events.PropertyChangeEvent")]
	public class FormTextInput extends mx.controls.TextInput implements FormInput
	{
		protected var formContainer:FormContainer;
		protected var topContainer:DisplayObject;
		protected var _property:String;
		
		public function FormTextInput()
		{
			super();
			topContainer = this;
			topContainer.addEventListener(FlexEvent.ADD, addListener);
			
			width = 160;
		}
		
		protected function addListener(event:FlexEvent):void {
			topContainer.removeEventListener(FlexEvent.ADD, addListener);
			
			while(true) {
				if (topContainer.parent is FormContainer) {
					formContainer = topContainer.parent as FormContainer;
					formContainer.addElement(this);
					break;
				} else if (topContainer.parent == null) {
					topContainer.addEventListener(FlexEvent.ADD, addListener);
					break;
				}
				topContainer = topContainer.parent;
			}
		}
		
		public function get elementValue():Object
		{
			if (MetaDataUtils.typeIs(validationHelper.type, Number) && (!valid || text == '')) {
				return NaN;
			} else if (!valid) {
				return null;
			} else { 
				return text;
			}
		}
		
		public function set elementValue(value:Object):void
		{
			if (value == null || value is String) {
				text = value as String;
			} else if (value is Number && isNaN(value as Number)) {
				text = "";
			} else {
				text = value.toString();
			}
			validationHelper.setNotValidated();
		}
		
		public function get property():String
		{
			return _property;
		}
		
		public function set property(property:String):void
		{
			_property = property;
		}
		
		
		override public function set data(value:Object):void {
			if (listData is DataGridListData) {
				elementValue = FlexUtils.getNestedProperty(value, DataGridListData(listData).dataField);
			} else {
				elementValue = value;
			}
			dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
		}
		
	// VALIDATION
		protected var validationHelper:InputValidationHelper = new InputValidationHelper(this as FormInput, "text", Event.CHANGE, FlexEvent.VALUE_COMMIT);
		
		[Bindable("validChanged")]
		public function get valid():Boolean {
			return validationHelper.valid;
		}
		
		public function isInvalidRequired():Boolean {
			return validationHelper.isInvalidRequired();
		}
		
		public function clearValidation():void {
			errorString = "";
		}
		
		public function set type(value:Class):void {
			validationHelper.type = value;
		}
		
		public function setMetaData(metaData:Object):void {
			validationHelper.setMetaData(metaData);
		}
		
		public function set notEmpty(value:Boolean):void {
			validationHelper.setMetaDataProp("notEmpty", value ? true : undefined);
		}
		
		public function set maxLength(value:Object):void {
			validationHelper.setMetaDataProp("maxLength", value != null ? value : undefined);
		}
		
		public function set minLength(value:Object):void {
			validationHelper.setMetaDataProp("minLength", value != null ? value : undefined);		
		}
		
		public function set maxValue(value:Object):void {
			validationHelper.setMetaDataProp("maxValue", value != null ? value : undefined);
		}
		
		public function set minValue(value:Object):void {
			validationHelper.setMetaDataProp("minValue", value != null ? value : undefined);
		}
		
		public function set email(value:Boolean):void {
			validationHelper.setMetaDataProp("email", value ? true : undefined);		
		}
		
		public function set regex(value:String):void {
			validationHelper.setMetaDataProp("regex", value ? value : undefined);
		}
		
		public function set regexFlags(value:String):void {
			validationHelper.setMetaDataProp("regexFlags", value ? value : undefined);
		}
	}
}